package com.example.wc.logindemo.getdownload;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.wc.logindemo.R;
import com.example.wc.logindemo.VolleyRequestDemo;

import java.util.List;

/**
 * Created by hui on 2017/3/24.
 */

public class GetDownloadFragment extends Fragment {

    private View fragmentView;
    private RecyclerView mPhotoRecyclerView;
    private PhotoAdapter mAdapter;

    private RequestQueue mQueue;
    private VolleyRequestDemo mVolleyRequestDemo;
    private ImageLab.Images mImages;
    private List<Image> mImageList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (fragmentView != null) {
            return fragmentView;
        }
        fragmentView = inflater.inflate(R.layout.fragment_get_download, container, false);

        mPhotoRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.photo_recycler_view);
        mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));

        //
        if(mVolleyRequestDemo == null){
            mVolleyRequestDemo = new VolleyRequestDemo();
        }
        mVolleyRequestDemo.volleyRequestDemo_GET_Image(GetDownloadFragment.this.getContext(),
                new VolleyRequestDemo.ImageGetListener() {
                    @Override
                    public void onImageGeted(ImageLab.Images images) {
                        //这里是子线程，子线程还未结束，调用了更新界面
                        mImages = images;
                        Log.d("GET_TAG", mImages.toString());
                        mImageList = mImages.data;//拍照旋转时会造成ACCESS丢失，mImageList变为0

                        if(mAdapter == null){
                            mAdapter = new PhotoAdapter(GetDownloadFragment.this.getContext(), mImageList);
                            mPhotoRecyclerView.setAdapter(mAdapter);
                        }else{
                            mAdapter.notifyDataSetChanged();
                        }

                    }
                });



        return fragmentView;
    }


}
